<?php

class PlatformSzSdk extends  PlatformBase
{
    /**
     * @var PlatformSzSdk
     */
    private static $instance;

    private $apiUrl;
    private $cpId;
    private $cpKey;
    private $cpGameId;

    /**
     * Get the instance of PlatformSzSdk.
     *
     * @return PlatformSzSdk
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new PlatformSzSdk();
        }
        return self::$instance;
    }

    private $cpInfoMap = [
        "1069" => [
            "cpId" => 7,
            "cpKey" => "!iCIzh8GeGkn",
            "apiUrl" => "https://api-test.shinezone.com/2.1/%s/?client=web"
        ],
        "1071" => [
            "cpId" => 7,
            "cpKey" => "!iCIzh8GeGkn",
            "apiUrl" => "https://api-test.shinezone.com/2.1/%s/?client=web"
        ],
        "100001" => [
            "cpId" => 1001,
            "cpKey" => "5bf3c06f7d45de257fd81919e5d180cc",
            "apiUrl" => "https://api-staging.shinezone.com/2.1/%s/?client=web"
        ],
        "200001" => [
            "cpId" => 2001,
            "cpKey" => "1e72c7bae2540b2785612e0e09a589e3",
            "apiUrl" => "https://api-review.shinezone.com/2.1/%s/?client=web"
        ],
        "300001" => [
            "cpId" => 3001,
            "cpKey" => "8466e26a7ce156d87e4efaa91efd95ab",
            "apiUrl" => "https://api.shinezone.com/2.1/%s/?client=web"
        ],
    ];

    private function __construct()
    {
        $gameSetting = $env = SzConfig::get()->loadAppConfig('app');
        if ($gameSetting['ENV'] == 'DEV') {//内网测试环境使用1069
            $gameSetting['CP_GAME_ID'] = 1069;
        }
        if (isset($gameSetting['CP_GAME_ID']) && isset($this->cpInfoMap[$gameSetting['CP_GAME_ID']])) {
            $this->cpGameId = $gameSetting['CP_GAME_ID'];
            $this->apiUrl = $this->cpInfoMap[$this->cpGameId]['apiUrl'];
            $this->cpId = $this->cpInfoMap[$this->cpGameId]['cpId'];
            $this->cpKey = $this->cpInfoMap[$this->cpGameId]['cpKey'];
        } else {
            throw new SzException(20046, 'cpGameId error: ' . $gameSetting['CP_GAME_ID']);
        }
    }

    /**
     * 验证token
     *
     * @param string $platformId  平台ID
     * @param string $token 登录token
     * @throws SzException
     * @return bool
     */
    public function checkToken($platformId, $token)
    {
        $apiUrl = sprintf($this->requestUrl, 'account/check_token');
        $result = PlatformService::postHttpRequest($apiUrl, ['token' => $token]);
        if (!SzUtility::checkArrayKey('code', $result) || $result['code'] != 0) {
            throw new SzException(20047, [$result['code'], $result['msg']]);
        }
        if (!isset($result['data']['sdk_id']) || $result['data']['sdk_id'] != $platformId) {
            throw new SzException(20048, [json_encode($result), $platformId]);
        }
        return true;
    }

    public function getCpKey()
    {
        return $this->cpKey;
    }
}